define(
    ["PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect",
        "PDUtilDir/fileupload/singleFileUpload", "PDUtilDir/wangEditor.min",
        "AppPathDir/utils", "PDUtilDir/validator", "Date", "DateCN",
        "css!DateCss", "css!AppPathDir/product/productInfos/views/css/css"],
    function (Util, Tool, Slidebar, inputSelect, MFU, E, Common, validator) {
        function addSupportInit($scope, $compile) {
            return new addSupport($scope, $compile);
        }

        function addSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
            this.validator = validator;
        }

        addSupport.fn = addSupport.prototype;

        addSupport.fn.init = function () {
            this.entity = {
                trip: [],
                timetable: new Array(),
                explain: {},
                image: {}
            };
            this.cache = {
                feature_editor: undefined,
                guarantee_editor: undefined,
                explainNotes_editor: undefined,
                explainCosts_editor: undefined,
                customTripName_editor: undefined
            };
            this.bind = {
                trip: {},
                tripTimestampArray_tmp: new Array(),
                tripMap: {},
                timetable: {}
            };
        };

        addSupport.fn.digest = function () {
            this.$scope.$digest();
        };
        addSupport.fn.run = function () {
            var _this = this;
            _this.init();
            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/product/productInfos/views/addProduct.html",
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        addSupport.fn.initialization = function () {
            this.baseInit();
            // this.tripInit();
        };

        addSupport.fn.baseInit = function () {
            var _this = this;
            // 加载code
            $.ajax({
                url: getServer() + "/flea/hclv/getUUID.htmls",
                method: 'get',
                success: function (data) {
                    _this.entity.code = data.data;
                    _this.digest();
                }
            });
            // 类型初始化下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "typeName",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.$scope.cache.types,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    var id = id[0];
                    _this.entity.type = id;
                    _this.$scope.addProductForm.type.$setViewValue(id);
                    _this.queryAllLines(id);
                }
            });

            // 初始化出发地省下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "add_startPlaceProvince",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.$scope.cache.provinces,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    var id = id[0];
                    _this.entity.startPlaceProvince = id;
                    _this.$scope.addProductForm.startPlaceProvince.$setViewValue(id);
                    _this.getCity('start', id);
                }
            });

            // 初始化目的地省下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "add_endPlaceProvince",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.$scope.cache.provinces,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    var id = id[0];
                    _this.entity.endPlaceProvince = id;
                    _this.$scope.addProductForm.endPlaceProvince.$setViewValue(id);
                    _this.getCity('end', id);
                }
            });
            // 初始化产品类别下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "add_productType",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: [{
                    "dictCode": "self",
                    "dictName": "自营"
                }, {
                    "dictCode": "unself",
                    "dictName": "非自营"
                }],
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    _this.entity.productCategory = id[0];
                    _this.$scope.addProductForm.productCategory.$setViewValue(id[0]);
                }
            });

            // 宣传图片
            MFU.init({
                placeAt: "id_img",
                allowDelete: true,
                allowMC: true,
                onUploadSuccess: function (file) {
                    _this.entity.image = $.extend(_this.entity.image, {
                        'resouceId': file.swordFileId,
                        'path': file.swordFilePath
                    });
                },
                onDeleteSavedFile:function(file){
                    if(file == null)
                        return;
                    _this.entity.image = undefined;
                    _this.digest();
                },
                onFileQueued: function (file) {
                    if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                        Util.alert(ImageSuffixNotSupport);
                        this.remove();
                        return;
                    }
                    this.webUploader.makeThumb(file, function (error, src) {
                        if (error) {
                            Util.alert('不能预览');
                            return;
                        }
                        _this.entity.image.src = src;
                        _this.digest();
                    }, 500, 300);
                    this.webUploader.upload();
                }
            });
            // 上传行程文档
            MFU.init({
                placeAt: "trip_doc_file",
                allowDelete: true,
                allowMC: true,
                onUploadSuccess: function (file) {
                    _this.entity.fileTripId = file.swordFileId;
                    _this.$scope.addTripFileUploadForm.fileTripId.$setViewValue(_this.entity.fileTripId);
                },
                onDeleteSavedFile:function(file){
                    if(file == null)
                        return;
                    _this.entity.fileTripId = undefined;
                    _this.$scope.addTripFileUploadForm.fileTripId.$setViewValue(_this.entity.fileTripId);
                    _this.digest();
                },
                onFileQueued: function (file) {
                    if(DocSuffix.indexOf(file.ext.toLowerCase()) < 0){
                        Util.alert(DocSuffixNotSupport);
                        this.remove();
                        return;
                    }
                    this.webUploader.upload();
                }
            });
            //特色福文本框
            _this.cache.feature_editor = new E('#feature');
            _this.cache.feature_editor.customConfig.onchange = function (html) {
                _this.entity.feature = html;
                _this.$scope.addProductForm.feature.$setViewValue(html);
            };
            _this.cache.feature_editor.customConfig.menus = Common.wangEditorMenus;
            _this.cache.feature_editor.create();

            //服务保障福文本框
            _this.cache.guarantee_editor = new E('#guarantee');
            _this.cache.guarantee_editor.customConfig.onchange = function (html) {
                _this.entity.guarantee = html;
                _this.$scope.addProductForm.guarantee.$setViewValue(html);
            };
            _this.cache.guarantee_editor.customConfig.menus = Common.wangEditorMenus;
            _this.cache.guarantee_editor.create();

            //自定义行程福文本框
            // _this.cache.customTripName_editor = new E('#customTripName');
            // _this.cache.customTripName_editor.customConfig.onchange = function (html) {
            //     _this.entity.customTrip = html;
            //     _this.$scope.addTripCustomForm.customTrip.$setViewValue(html);
            // };
            // _this.cache.customTripName_editor.customConfig.menus = ['image'].concat(Common.wangEditorMenus);
            // _this.cache.customTripName_editor.customConfig.uploadImgShowBase64 = true
            // _this.cache.customTripName_editor.create();

            //预定须知福文本框
            _this.cache.explainNotes_editor = new E('#explainNotes');
            _this.cache.explainNotes_editor.customConfig.onchange = function (html) {
                _this.entity.explain.notes = html;
                _this.$scope.addEXPLAINForm.explainNotes.$setViewValue(html);
            };
            _this.cache.explainNotes_editor.customConfig.menus = Common.wangEditorMenus;
            _this.cache.explainNotes_editor.create();

            //费用说明福文本框
            _this.cache.explainCosts_editor = new E('#explainCosts');
            _this.cache.explainCosts_editor.customConfig.onchange = function (html) {
                _this.entity.explain.costs = html;
                _this.$scope.addEXPLAINForm.explainCosts.$setViewValue(html);
            };
            _this.cache.explainCosts_editor.customConfig.menus = Common.wangEditorMenus;
            _this.cache.explainCosts_editor.create();
        };
        //获取出发城市
        addSupport.fn.getCity = function (type, pcode) {
            var _this = this;
            $.ajax({
                url: getServer() + "/flea/hclv/getCity.htmls",
                data: {
                    'pcode': pcode
                },
                success: function (data) {
                    var citys = data.data;
                    if (type == "start") {
                        _this.cache.startPlaceCity = citys;
                        _this.startPlaceCity();
                    } else {
                        _this.cache.endPlaceCity = citys;
                        _this.endPalceCity();
                    }
                }
            });
        };
        //出发城市下拉框初始化
        addSupport.fn.startPlaceCity = function () {
            var _this = this;
            // 初始化下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "add_startPlaceCity",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.cache.startPlaceCity,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    var id = id[0];
                    _this.entity.startPlaceCity = id;
                    _this.$scope.addProductForm.startPlaceCity.$setViewValue(id);
                }
            });
        };
        //目的城市下拉框初始化
        addSupport.fn.endPalceCity = function () {
            var _this = this;
            // 初始化下拉框
            inputSelect.dropCache();
            inputSelect({
                id: "add_endPalceCity",
                key: {
                    id: "dictCode",
                    name: "dictName",
                    data: "data",
                },
                data: _this.cache.endPlaceCity,
                simpleData: true,
                searchAble: false,
                callback: function (id) {
                    var id = id[0];
                    _this.entity.endPalceCity = id;
                    _this.$scope.addProductForm.endPalceCity.$setViewValue(id);
                }
            });
        };
        addSupport.fn.init_fileupload = function () {
            var _this = this, len = _this.entity.trip.length;
            if (!_this.cache.isRender)
                return;
            for (var i = 0; i < len; i++) {
                var obj = {
                    placeAt: "id_" + (i + 1),
                    allowDelete: true,
                    allowMC: true,
                    onUploadSuccess: function (file) {
                        var id = this.settings.placeAt.substring(3) - 1, obj = _this.entity.trip[id];
                        obj.image = $.extend(obj.image, {
                            'resouceId': file.swordFileId,
                            'path': file.swordFilePath
                        });
                    },
                    onDeleteSavedFile: function (file) {
                        if(file == null)
                            return;
                    },
                    onFileQueued: function (file) {
                        if(ImageSuffix.indexOf(file.ext.toLowerCase()) < 0){
                            Util.alert(ImageSuffixNotSupport);
                            this.remove();
                            return;
                        }
                        var id = this.settings.placeAt.substring(3) - 1, webUploader = this.webUploader;
                        webUploader.makeThumb(file, function (error, src) {
                            if (error) {
                                Util.alert('不能预览');
                                return;
                            }
                            _this.entity.trip[id].image.src = src;
                            _this.digest();
                        }, 500, 300);
                        webUploader.upload();
                    }
                };
                MFU.init(obj);
            }
            _this.cache.isRender = false;
        };

        //初始化行程
        addSupport.fn.tripInit = function () {
            var _this = this;
            _this.$scope.$watch('addSupport.entity.travelDay', function (newValue, oldValue) {
                _this.entity.trip = new Array();
                for (var i = 0; i < newValue; i++) {
                    _this.entity.trip.push({
                        sort: (i + 1),
                        vehicle: {
                            bus: false,
                            train: false,
                            ship: false,
                            subway: false,
                            hsubway: false,
                            plane: false
                        },
                        image: {},
                        meal: {
                            breakfast: false,
                            lunch: false,
                            dinner: false,
                            nothing: false,
                            aircraft: false
                        },
                        describe: undefined,
                        stayDescribe: undefined,
                        isAbroad: false
                    });
                }
                _this.cache.isRender = true;
            });
        };
        addSupport.fn.save = function () {
            var _this = this;
            for (var item in _this.entity.trip) {
                _this.entity.trip[item].image.src = undefined;
                if (_this.entity.trip[item].isAbroad == "true") {
                    _this.entity.trip[item].isAbroad = true;
                } else {
                    _this.entity.trip[item].isAbroad = false;
                }
            }
            _this.entity.image.src = undefined;
            $.ajax({
                url: getServer() + "/flea/hclv/product/save.htmls",
                data: {
                    'model': JSON.stringify(_this.entity)
                },
                success: function (data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.$scope.grid.refresh();
                    Util.alert('成功');
                    _this.slidebars.close();
                }
            });
        };
        addSupport.fn.queryAllLines = function (type) {
            var _this = this;
            $.ajax({
                url: getServer() + "/flea/hclv/line/queryAll.htmls",
                data: {
                    type: type
                },
                success: function (data) {
                    _this.refreshLine(data.data);
                }
            });
        };
        addSupport.fn.refreshLine = function (data, init) {
            var _this = this;
            inputSelect.dropCache();
            inputSelect({
                id: "lineName",
                key: {
                    id: "id",
                    name: "name",
                    data: "data",
                },
                data: data,
                simpleData: true,
                // 启用文本搜索
                initData: [init],
                searchAble: false,
                callback: function (id, data) {
                    _this.entity.lineId = id[0];
                    _this.$scope.addProductForm.lineId.$setViewValue(id[0]);
                }
            });
        };
        return addSupportInit;
    });